package com.example.simplefactory.factoryjd;

import com.example.simplefactory.DingdingMessage;
import com.example.simplefactory.IMessage;
import com.example.simplefactory.QqMessage;
import com.example.simplefactory.WeChatMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0.0
 * @className: SayHelloFactory
 * @description:
 * @author: zhangjunfa
 * @date: 2022/6/9 14:02
 */
public class SayHelloFactory {
    private final static Map<String, Object> map = new HashMap<>();

    /**
     * 将接口定义的实现方式存储到map中，方便获取
     */
    static {
        map.put("wechat", new WeChatMessage());
        map.put("qq", new QqMessage());
        map.put("dingding", new DingdingMessage());
    }


    /**
     * 通过类型获取工厂类实例
     * 注意：如果获取不到对应实例，要做空异常判断
     * @param type
     * @return
     */
    public static IMessage createSendMessage(String type) {
        return (IMessage) map.getOrDefault(type, null);
    }
}
